<template>
  <Main :type="'form'">
    <el-form ref="form" label-width="150px" :model="form" :rules="form">
      <Title :title="'抵扣设置'" />
      <el-form-item label="可用人群">
        <el-radio v-model="form.member_auth" :label="1">会员</el-radio>
      </el-form-item>
      <el-form-item label="单次使用限制">
        <div>
          <el-checkbox v-model="form.is_limit_min">最少使用</el-checkbox
          >&nbsp;&nbsp;
          <el-input-number
            v-model="form.deduction_limit_min"
            :min="1"
            :precision="0"
            :disabled="!form.is_limit_min"
            :controls="false"
          ></el-input-number
          >&nbsp;&nbsp;抵扣金
        </div>
        <div style="margin-top:20px;">
          <el-checkbox v-model="form.is_limit_max">最多使用</el-checkbox
          >&nbsp;&nbsp;
          <el-input-number
            v-model="form.deduction_limit_max"
            :min="1"
            :precision="0"
            :disabled="!form.is_limit_max"
            :controls="false"
          ></el-input-number
          >&nbsp;&nbsp;抵扣金
        </div>
      </el-form-item>
      <el-form-item label="抵扣项目">
        <el-checkbox-group v-model="form.deduction_type">
          <el-checkbox label="1">订单金额</el-checkbox>
          <el-checkbox label="2">订单运费</el-checkbox>
        </el-checkbox-group>
        <span class="add-info">纯运费金额，与商品无关</span>
      </el-form-item>
      <el-form-item label="适用商品">
        <el-radio-group v-model="form.apply_type" @change="DataArr = []">
          <el-radio :label="0">全部商品参与</el-radio>
          <el-radio :label="1">指定商品参与</el-radio>
          <el-radio :label="2">指定商品不参与</el-radio>
        </el-radio-group>
        <div v-if="form.apply_type != 0">
          <shop-new :data-arr="DataArr" @close="close" />
        </div>
      </el-form-item>
      <el-form-item label="订单抵扣比例">
        <el-checkbox v-model="form.is_deduction_bili"
          >单次消费最多抵扣订单金额的</el-checkbox
        >&nbsp;&nbsp;
        <el-input-number
          v-model="form.deduction_bili"
          :min="1"
          :max="100"
          :precision="0"
          :disabled="!form.is_deduction_bili"
          :controls="false"
        ></el-input-number
        >&nbsp;&nbsp;%
      </el-form-item>
    </el-form>
    <Footer>
      <el-button type="primary" @click="submit">保 存</el-button>
    </Footer>
  </Main>
</template>
<script>
import { SETDEDUCTION } from "../../service";

export default {
  components: {
    shopNew: () => import("publicCom/shopNew")
  },
  data() {
    return {
      dialogVisible: false,
      dataJson: {},
      DataArr: [],
      form: {
        member_auth: 1,
        is_limit_min: false,
        is_limit_max: false,
        deduction_limit_min: "",
        deduction_limit_max: "",
        deduction_type: [],
        is_deduction_bili: false,
        deduction_bili: "",
        apply_type: 0
      }
    };
  },
  created() {
    this.get();
  },

  methods: {
    close(e) {
      if (e) {
        this.DataArr = e;
      }
    },
    async submit() {
      let arr = [];
      this.DataArr.forEach(ele => {
        arr.push(ele.goods_id);
      });
      await this.$refs["form"].validate(valid => {
        if (valid) {
          SETDEDUCTION({
            flag: "save",
            member_auth: this.form.member_auth,
            is_limit_min: this.form.is_limit_min ? 1 : 0,
            deduction_limit_min: this.form.deduction_limit_min,
            is_limit_max: this.form.is_limit_max ? 1 : 0,
            deduction_limit_max: this.form.deduction_limit_max,
            deduction_type: this.form.deduction_type.join(","),
            is_deduction_bili: this.form.is_deduction_bili ? 1 : 0,
            deduction_bili: this.form.deduction_bili,
            apply_type: this.form.apply_type,
            apply_goods: arr.join(",")
          }).then(res => {
            let { code, msg } = res;
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        }
      });
    },
    async get() {
      await SETDEDUCTION().then(res => {
        let { code, data, msg } = res;
        if (code === 200) {
          if (!data) return;
          this.form = {
            member_auth: data.member_auth,
            is_limit_min: data.assets_rule.is_limit_min === 1,
            deduction_limit_min: data.assets_rule.deduction_limit_min,
            is_limit_max: data.assets_rule.is_limit_max === 1,
            deduction_limit_max: data.assets_rule.deduction_limit_max,
            deduction_type: data.assets_rule.deduction_type
              ? data.assets_rule.deduction_type.split(",")
              : [],
            is_deduction_bili: data.assets_rule.is_deduction_bili === 1,
            deduction_bili: data.assets_rule.deduction_bili,
            apply_type: data.assets_rule.apply_type
          };
          this.DataArr = data.assets_rule.goods;
        } else {
          this.$message.error(msg);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped></style>
